import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/evaluation.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionButtonCreateEvaluationHomework.dart';
import 'package:ichinesereader/widgets/forms/levelChoiceChipEvaluation.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBoxTeaching.dart';

class EvaluationBooksTeacherPage extends StatefulWidget {
  static const String routeName = '/teacher/books/evaluation';

  @override
  State<EvaluationBooksTeacherPage> createState() =>
      _EvaluationBooksTeacherPageState();
}

class _EvaluationBooksTeacherPageState
    extends State<EvaluationBooksTeacherPage> {
  final EvaluationController _controller = Get.find();
  final box = Hive.box('appconfig');
  int _currentLevel = 1;

  @override
  void initState() {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    String classId = box.get(CacheManagerKey['currentclass'].toString());
    if (isTeacher) {
      try {
        _controller.selectedEvaluationBooks.clear();
        _controller.evaluationBooks.clear();
         _currentLevel = (box.get(CacheManagerKey['eveluationlevel'].toString(),
                                defaultValue: 1) as double)
                            .toInt();
      } catch (error) {
        _currentLevel = 1;
        box.put(CacheManagerKey['eveluationlevel'].toString(), 1.0);
      }
      Future.delayed(Duration(milliseconds: 200), (() {
        _controller.fetchEvaluationBooks(
            '0', '0', 'EVALUATION', _currentLevel.toString(), classId);
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build evaluation Books TeacherPage');
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          LevelChioceChipEvaluationWidget(),
          SizedBox(height: 9),
          Flexible(
              child: _controller.obx(
            (data) => BookGridWithCheckBoxTeaching(
              books: _controller.evaluationBooks,
              readingType: NanhaiBookReadingTypes.evaluation,
            ),
            onEmpty: Center(child: Text('Coming soon ...'.tr)),
            onLoading: Container(),
          )),
        ],
      ),
      floatingActionButton: FloatingActionButtonCreateEvaluationHomework(),
    );
  }
}
